<?php
namespace Futures\Controller;
use Common\Model\OptionModel;
/**
 *  自动更新执行的控制器
 */
class InsertDataController{


    public function insert_data(){

        $scode = M('FuturesOption')->where(array('status'=>1,'type'=>1))->getField('code');
        $scode = 'HIHSI06';
        $ecode = M('FuturesOption')->where(array('status'=>1,'type'=>2))->getField('code');
        $code = $scode.','.$ecode;
        $output = get_data_to_insert($code);
        $data = nl2br($output);//将分行符"\r\n"转义成HTML的换行符"<br />"
        $datas = explode("<br />",$data);//"<br />"作为分隔切成数组
        $this->insert_euro_data($datas[1]);
        $this->insert_stock_data($datas[0]);
    }

    /**
     * [insert_euro_data 插入欧指行情数据]
     * @return [type] [description]
     */
    public function insert_euro_data($output){

         //然后判断时候是当日交易时间段
        // $one_start = mktime(15,00,00,date('m'),date('d'),date('Y'));
        // $one_end = mktime(24,00,00,date('m'),date('d'),date('Y'));
        
        // $two_start = mktime(00,00,00,date('m'),date('d'),date('Y'));
        // $two_end = mktime(04,00,00,date('m'),date('d'),date('Y'));
              
        // if(!(((time()+$duration)>$one_start&&(time()+$duration)<$one_end)||((time()+$duration)>$two_start&&(time()+$duration)<$two_end) || ((time()+$duration)>$three_start&&(time()+$duration)<$three_end))){
        //     $this->error('不在交易时段');exit;
        // }
        // if(!((time()>$one_start&&time()<$one_end)||(time()>$two_start&&time()<$two_end))){
        //      echo '欧元不在交易时段'; return;
        // }

         $Mongo= new OptionModel('optiondatas');
        // $option = M('FuturesOption')->where(array('status'=>1,'type'=>2))->find();
        // $output = get_data_to_insert($option['code']);
        $datas = explode(',',$output);
        if($output){
            $data['date_time'] = $datas[0];
            $data['symbol'] = $datas[1];
            $data['name'] = $datas[2];
            // $data['open_price'] = $datas[8];
            // $data['last_price'] = $datas[11];
            // $data['buy_price'] = $datas[14];
            // $data['sell_price'] = $datas[24];
            $datas[14] = $datas[14]-0.00005;
            $datas[24] = $datas[24]+0.00005;
            $data['open_price'] = sprintf("%.5f",$datas[8]);
            $data['last_price'] = sprintf("%.5f",$datas[11]);
            $data['buy_price'] = sprintf("%.5f",$datas[14]);
            $data['sell_price'] = sprintf("%.5f",$datas[24]);
            $data['high_price'] = sprintf("%.5f",$datas[9]);
            $data['low_price'] = sprintf("%.5f",$datas[10]);
            $data['buy_amount'] = sprintf("%.0f",$datas[19]);
            $data['sell_amount'] = sprintf("%.0f",$datas[29]);

            $data['type'] =2;   //1是恒指，2是欧指
            $data['create_time'] = time();
            if($data['open_price']<=0) die;
            $res = $Mongo->add($data);
            if(false === $res){
                $file='./Data/optiondatas.log';
                $error = $Mongo->$Model->getDbError();
                $content = "插入欧指数据失败：".$error;
                file_put_contents($file,$content, FILE_APPEND);
                echo '失败';
            }else{
                $map['create_time'] = array('lt',time()-1800);
                $map['type'] = 2;
                $Mongo->where($map)->delete();
            }
        }   
       
    }

    public function insert_stock_data($output){

         //然后判断时候是当日交易时间段
        // $one_start = mktime(9,15,00,date('m'),date('d'),date('Y'));
        // $one_end = mktime(12,00,00,date('m'),date('d'),date('Y'));
        
        // $two_start = mktime(13,00,00,date('m'),date('d'),date('Y'));
        // $two_end = mktime(16,15,00,date('m'),date('d'),date('Y'));

        // $three_start = mktime(17,00,00,date('m'),date('d'),date('Y'));
        // $three_end = mktime(23,45,00,date('m'),date('d'),date('Y'));
        
        // if(!(((time()+$duration)>$one_start&&(time()+$duration)<$one_end)||((time()+$duration)>$two_start&&(time()+$duration)<$two_end) || ((time()+$duration)>$three_start&&(time()+$duration)<$three_end))){
        //     $this->error('不在交易时段');exit;
        // }
        // if(!((time()>$one_start&&time()<$one_end)||(time()>$two_start&&time()<$two_end) || (time()>$three_start&&time()<$three_end))){
        //     echo '恒指不在交易时段'; return;
        // }

         $Mongo= new OptionModel('optiondatas');
        // $option = M('FuturesOption')->where(array('status'=>1,'type'=>1))->find();
        // $option['code'] = 'HIHSI04';
        // $output = get_data_to_insert($option['code']);
        $datas = explode(',',$output);
        //dump($datas);
        if($output){
            $data['date_time'] = $datas[0];
            $data['symbol'] = $datas[1];
            $data['name'] = $datas[2];
            $data['open_price'] = sprintf("%.2f",$datas[8]);
            $data['last_price'] = sprintf("%.2f",$datas[11]);
            $data['buy_price'] = sprintf("%.2f",$datas[14]);
            $data['sell_price'] = sprintf("%.2f",$datas[24]);
            $data['high_price'] = sprintf("%.2f",$datas[9]);
            $data['low_price'] = sprintf("%.2f",$datas[10]);
            $data['buy_amount'] = sprintf("%.0f",$datas[19]);
            $data['sell_amount'] = sprintf("%.0f",$datas[29]);
            $data['type'] =1;   //1是恒指，2是欧指
            $data['create_time'] = time();
            if($data['open_price']<=0) die;
            $res = $Mongo->add($data);
            if(false === $res){
                $file='./Data/optiondatas.log';
                $error = $Mongo->$Model->getDbError();
                $content = "插入欧指数据失败：".$error;
                file_put_contents($file,$content, FILE_APPEND);
                echo '失败';
            }else{
                $map['create_time'] = array('lt',time()-1800);
                $map['type'] = 1;
                $Mongo->where($map)->delete();
            }
        }   
       
    }


    public function get_data(){
        //$Mongo= new OptionModel('optiondatas');
        //$data = $Mongo->order('id desc')->limit(1)->select();
        $data = get_euro_data();
        dump($data);
    }


    public function fenshi(){
        //if(empty($code)) return false;
        // $url = "http://quote.jiaoyidaren.com/quote?code={$code}&_=".time();
        $url = 'http://db2015.wstock.cn/wsDB_API2/TheTimeTrend.php?r_type=0&symbol=HIHSI04&u=fivip100&p=yixi888';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate, sdch");  
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        if(empty($output)) return false;
        
        $data = nl2br($output);//将分行符"\r\n"转义成HTML的换行符"<br />"
        $datas = explode("<br />",$data);//"<br />"作为分隔切成数组
        //dump($datas);
        $arr = array();
        foreach($datas as $k=>$val){
            //dump($val);
            $vs = explode(',',$val);
            if(!empty($vs[4])){
                $newArr[$k]['ys'] = (float)$vs[4];    //均价
                //$newArr[$k]['Open_Int'] = $vs[6];       //持仓量
                //$newArr[$k]['volume'] = $vs[7];       //总成交量
                //$newArr[$k]['amount'] = $vs[8];       //总成总额
                if($k>0&&$datas[$k-1][4]>=$vs[4]){
                    $newArr[$k]['zs'] = (int)$vs[7];
                    $newArr[$k]['ds'] = 0;
                }else{
                    $newArr[$k]['zs'] = 0;
                    $newArr[$k]['ds'] = (int)$vs[7]; 
                }
            }
           

        }
        //$shishi = get_data_to_insert();
        //dump($shishi);
        $datas = explode(',',$shishi);
        $shi['close'] = $datas[7];
        $shi['open'] = $datas[8];
        $shi['hign'] = $datas[9];
        $shi['low'] = $datas[10];
        $arr['tu'] = $newArr;
        $arr['shi'] = $shi;
        //dump($newArr);
        echo json_encode($arr);
        //return $output;
    }
}